<template>
  <div class="com-page">
    <div class='com-chart' ref='today_yuqing_shuxing'></div>
  </div>
</template>

<script>
export default {
  name: "today_yuqing_shuxing",
  data() {
    return {
      chartInstance: null,
      allData: [],
      currentIndex: 0, // 当前显示的数据
      timerId: null // 定时器的标识
    }
  },
  mounted() {
    this.initChart()
    this.getData()

    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.today_yuqing_shuxing)


      // 从服务器端获取数据
      const {data: ret} = await this.$http.get('/index')
      // console.log("今日数据属性",ret.data.todyemtionTrend)
      this.allData = ret.data.todyemtionTrend
      let testSeries = this.allData.map((item, index) => {
        console.log(item)
        let qinggan = ""
        if (item.emo == "POS") {
          qinggan = "积极"
        } else if (item.emo == "NEG") {
          qinggan = "消极"
        } else {
          qinggan = "中性"
        }
        return {
          name: qinggan,
            value: item.value,
        }
      })

      const initOption = {
        title: {
          text: '▎今日舆情属性',
          left: 20,
          top: 20,
          textStyle: {
            color: "#fff"
          }
        },
        label: {
          show: true,
          formatter: function (arg) {
            // console.log(arg)
            return arg.name + " " + arg.value + "条\n" + arg.percent + "%"
          },
        },
        series: {
          radius: '55%',
          type: "pie",
          data: testSeries,
          // radius: this.titleFontSize * 4.5,
          // center: ['50%', '60%']
        },
        color:[ //自定义的颜色
          'rgb(60,163,60)',
          'rgb(180,52,52)',
          'rgb(163,111,61)',
        ]
      }
      this.chartInstance.setOption(initOption)
    },
    async getData() {
      // 获取服务器的数据, 对this.allData进行赋值之后, 调用updateChart方法更新图表
      const {data: ret} = await this.$http.get('/index')
      this.allData = ret.data.todyemtionTrend
      // console.log("todyemtionTrend==>", this.allData)
      this.updateChart()
      // this.startInterval()
    },
    updateChart() {

      this.chartInstance = this.$echarts.init(this.$refs.today_yuqing_shuxing)

      let testSeries = this.allData.map((item, index) => {
        let qinggan = ""
        if (item.emo == "POS") {
          qinggan = "积极"
        } else if (item.emo == "NEG") {
          qinggan = "消极"
        } else {
          qinggan = "中性"
        }
        return {
          name: qinggan,
          value: item.count[0],
        }
      })

      console.log("testSeries", testSeries)

      // 设置dataoption
      const dataOption = {
        // series: seriesArr
        name:"test",
        series: [{
          type: 'pie',
          data: testSeries
        }]
      }
      this.chartInstance.setOption(dataOption)
    }
    ,
    screenAdapter() {
      const titleFontSize = this.$refs.today_yuqing_shuxing.offsetWidth / 100 * 3.6
      const innerRadius = titleFontSize * 2.8
      const outterRadius = innerRadius * 1.125
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        // series: [
        //   {
        //     type: 'pie',
        //     radius: [outterRadius, innerRadius],
        //     label: {
        //       fontSize: titleFontSize / 2
        //     }
        //   },
        //   {
        //     type: 'pie',
        //     radius: [outterRadius, innerRadius],
        //     label: {
        //       fontSize: titleFontSize / 2
        //     }
        //   },
        //   {
        //     type: 'pie',
        //     radius: [outterRadius, innerRadius],
        //     label: {
        //       fontSize: titleFontSize / 2
        //     }
        //   },
        //   {
        //     type: 'pie',
        //     radius: [outterRadius, innerRadius],
        //     label: {
        //       fontSize: titleFontSize / 2
        //     }
        //   },
        //   {
        //     type: 'pie',
        //     radius: [outterRadius, innerRadius],
        //     label: {
        //       fontSize: titleFontSize / 2
        //     }
        //   }
        // ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    }
    ,

  },
}
</script>

<style scoped>

</style>
